package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.entity.Article;
import com.orientation.service.ArticleService;
import com.orientation.utils.PageUtils;
import com.orientation.vo.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-3-9
 */

@Api(tags = "文章发布")
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertArticle(@RequestBody Article article) {
        articleService.insertArticle(article);
        return ResponseEntity.ok(new Result());
    }
    // public boolean save(@RequestBody Article article) {
    //     return articleService.insertArticle(article);
    //     // return articleService.save(article);
    // }

    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Article article) {
        return articleService.updateById(article);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return articleService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Article findById(@PathVariable String id) {
        return articleService.getById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Article> findAll() {
        return articleService.list();
    }

    //
    @ApiOperation("分页查询")
    @GetMapping("/page/pageCid")
    public IPage<Article> pageCid(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) String cid,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return articleService.page(new Page<>(pageNum, pageSize), Wrappers.<Article>lambdaQuery().like(StringUtils.hasLength(name), Article::getTitle, name).eq(Article::getCid, cid));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseEntity<Result> findPage(@RequestParam(required = false) String cid,
                                           @RequestParam(required = false, defaultValue = "") String name,
                                           @RequestParam(required = false, defaultValue = "") String newsTime,
                                           @RequestParam(required = false, defaultValue = "") String releaseType,
                                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        IPage<ArticleVo> articleVoIPage = articleService.queryArticleVo(pageNum, pageSize, cid, name, newsTime, releaseType);
        List<ArticleVo> articleVoStream = articleVoIPage.getRecords().stream().filter(articleVo -> !"4".equals(articleVo.getCid())).sorted(Comparator.comparing(ArticleVo::getReleaseType)).collect(Collectors.toList());
        articleVoIPage.setRecords(articleVoStream);
        return ResponseEntity.ok(new Result(articleVoIPage));
    }

    @ApiOperation("我的发布分页查询///无用")
    @GetMapping("/queryByMyArticle")
    public ResponseEntity<Result> queryByMyArticle(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Page<ArticleVo> articleVoPage = articleService.queryByMyArticle(pageNum, pageSize);
        return ResponseEntity.ok(new Result(articleVoPage));

    }

    @ApiOperation("首页文章查询")
    @GetMapping("/news")
    public List<Article> findNews(@RequestParam String name) {
        // 根据类型取10条
        return articleService.list(Wrappers.<Article>lambdaQuery().eq(StringUtils.hasLength(name), Article::getCid, name).orderByDesc(Article::getCreateTime).last("limit 10"));
    }

    @ApiOperation("首页公告")
    @GetMapping("/notice")
    public Article findNotice() {
        return articleService.getOne(Wrappers.<Article>lambdaQuery().eq(Article::getCid, 1).orderByDesc(Article::getCreateTime).last("limit 1"));
    }

    /**
     * 修改发布状态
     */
    @ApiOperation("修改启用状态")
    @PostMapping("/updateStateType")
    public ResponseEntity<Result> updateStates(@RequestBody Article article) {
        if (!ObjectUtils.isEmpty(article) && article.getReleaseType().equals("1")) {
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), zoneId);
            article.setNewsTime(localDateTime);
        }
        articleService.updateStates(article);
        return ResponseEntity.ok(new Result());
    }


    @ApiOperation("校园风光")
    @GetMapping("/ListSchool")
    public List<Article> queryListSchool() {
        return articleService.list(Wrappers.<Article>lambdaQuery().eq(Article::getCid, 4)).stream().sorted(Comparator.comparing(Article::getCreateTime)).collect(Collectors.toList());
    }

    @ApiOperation("条件校园风光")
    @GetMapping("/queryListSchool")
    public IPage<Article> queryListSchool(@RequestParam(required = false, defaultValue = "") String name,
                                          @RequestParam(required = false, defaultValue = "") String newsTime,
                                          @RequestParam(required = false, defaultValue = "") String createTime,
                                          @RequestParam(required = false, defaultValue = "") String releaseType,
                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        List<Article> articles = articleService.list(Wrappers.<Article>lambdaQuery().eq(Article::getCid, 4)
                .eq(StringUtils.hasLength(name), Article::getTitle, name)
                .eq(StringUtils.hasLength(releaseType), Article::getReleaseType, releaseType)
                .like(StringUtils.hasLength(newsTime), Article::getNewsTime, newsTime)
                .like(StringUtils.hasLength(createTime), Article::getCreateTime, createTime)).stream().sorted(Comparator.comparing(Article::getCreateTime)).collect(Collectors.toList());
        return PageUtils.getPage(articles, pageNum, pageSize);

    }

    @ApiOperation("添加校园风")
    @PostMapping("/insertSchool")
    public ResponseEntity<Result> insertSchool(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                              @RequestParam(value = "title") String title,
                                              @RequestParam(value = "author") String author) {
        articleService.insertSchool(files, title, author);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改校园风光")
    @PostMapping("/updateSchoolNew")
    public ResponseEntity<Result> updateSchoolNew(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                                  @RequestParam(value = "sid", required = true) String sid,
                                                  @RequestParam(value = "urls", required = false) List<String> urls,
                                                  @RequestParam(value = "title", required = false) String title,
                                                  @RequestParam(value = "author", required = false) String author) {
        articleService.updateSchoolNew(files, sid, urls, title, author);
        return ResponseEntity.ok(new Result());
    }

}